<?php

namespace Portal\Domain;

use Portal\Model\AdminTimes as AdminTimesModel;

class AdminTimes
{
    public function addTimes($username)
    {

        $ip = \PhalApi\Tool::getClientIp();
        $unique = array('username' => $username);
        $insert = array('username' => $username, 'ip' => $ip, 'login_time' => time(),'times' => 1);
        $update = array('ip' => $ip, 'login_time' => time(),'times' => new \NotORM_Literal("times + 1"));
        // 返回影响的行数
        return AdminTimesModel::notorm()->insert_update($unique, $insert, $update);
    }

    public function fluShTimes($username)
    {
        //刷新登陆错误次数
        AdminTimesModel::notorm()->where(['username' => $username])->delete();
    }
    public function checkLoginTime($username)
    {
        //更新错误登陆时间
        $adminTimesData = AdminTimesModel::notorm()->select('times,login_time')->where(['username' => $username])->fetchOne();
        if (!$adminTimesData) {
            $adminTimesData['times'] = 0;
            $adminTimesData['login_time'] = time();
        }
        return $adminTimesData;
    }
}
